import json
from urllib import request, parse
import re
import time
import random

from pip._vendor import requests

from ua_info import ua_list


# 爬取搜索页面
class JingDongPage(object):
    def __init__(self):
        self.url = 'https://search.jd.com/Search?keyword={}&enc=utf-8&pvid=be533512639e4c31822018e84f6b90fa&page='

    def get_html(self, url):
        headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) '
                                 'Chrome/94.0.4606.71 Safari/537.36'}
        req = request.Request(url=url, headers=headers)
        res = request.urlopen(req)
        html = res.read().decode()
        self.parse_html(html)

    # 解析函数
    def parse_html(self, html):
        # 正则表达式
        re_bds = '<li data-sku="(.*?)" data-spu=".*?" ware-type=".*?"   class="gl-item">'
        pattern = re.compile(re_bds, re.S)
        r_list = pattern.findall(html)
        self.save_html(r_list)

    def save_html(self, r_list):
        for r in r_list:
            dataAnalysis = DataAnalysis(r)
            dataAnalysis.run()

    # 主函数
    def run(self):
        word = input('请输入要搜索的内容:')
        # quote()只能对字符串进行编码
        query_string = parse.quote(word)
        self.url = self.url.format(query_string)
        for i in range(1):
            self.url = self.url + str(i + 1) + '&s=&click=0'
            print('第' + str(i + 1) + '页')
            self.get_html(self.url)
            # time.sleep(random.uniform(1, 2))


class DataAnalysis(object):
    def __init__(self, url):
        self.urlMessage = url
        # 获取评论的链接
        self.urlComments = 'https://club.jd.com/comment/productPageComments.action?callback=fetchJSON_comment98' \
                           '&productId=' + str(url)
        # 获取价格的链接
        self.urlPrice = 'https://item-soa.jd.com/getWareBusiness?callback=jQuery697146&skuId=' + str(url)
        # 获取商品的链接
        self.url = 'https://item.jd.com/' + str(url) + '.html'
        # 获取白条信息
        self.urlIous = 'https://item-soa.jd.com/getWareBusiness?callback=jQuery5205918&skuId=' + str(url)
        self.headers = {'User-Agent': random.choice(ua_list)}

    def get_html(self, url):
        req = request.Request(url=url, headers=self.headers)
        res = request.urlopen(req)
        html = res.read().decode()
        self.parse_html(html)

    def get_ious(self, url):
        res = requests.get(url=url, headers=self.headers)
        res = res.content.decode().replace("jQuery5205918(", "").replace(")", "")
        # res = json.dumps(res)
        json_response = json.loads(res)
        if len(json_response['whiteBarInfo']['planInfos']) > 0:
            print("白条分期：")
            for s in json_response['whiteBarInfo']['planInfos']:
                print('\t' + s['mainTitle'] + '   ' + s['secondTitle'].replace('&times;', '*'))

    def get_comments(self, url):
        res = requests.get(url=url + "&score=0&sortType=5&page=0&pageSize=10", headers=self.headers)
        res = res.content.decode('gb18030', 'ignore').replace("fetchJSON_comment98(", "").replace(");", "")
        if len(res) > 1:
            json_response = json.loads(res)
            print('评论总数量：' + json_response["productCommentSummary"]["commentCountStr"])
            print("用户好评率：" + str(json_response["productCommentSummary"]["goodRateShow"]) + '%')
            print("默认好评数：" + json_response["productCommentSummary"]["defaultGoodCountStr"])
            print("用户好评数：" + json_response["productCommentSummary"]["goodCountStr"])
            print("用户中评数：" + json_response["productCommentSummary"]["generalCountStr"])
            print("用户差评数：" + json_response["productCommentSummary"]["poorCountStr"])
            if len(json_response['comments']) > 0:
                data = open('D:\\'+str(self.urlMessage)+'.txt', 'w+')
                # print("评论内容：", file=data)
                for s in json_response['comments']:
                    print('评论指数：' + str(s['score']) + '   内容：' + s['content'].replace("\n", ''), file=data)
                if json_response['maxPage'] > 1:
                    k = 0
                    for i in range(json_response['maxPage']):
                        if k != 0:
                            res = requests.get(url=url + "&score=0&sortType=5&page=" + str(i) + "&pageSize=10",
                                               headers=self.headers)
                            res = res.content.decode('gb18030', 'ignore').replace("fetchJSON_comment98(", "").replace(
                                ");", "")
                            if len(res) > 1:
                                json_response = json.loads(res)
                                if len(json_response['comments']) > 0:
                                    for s in json_response['comments']:
                                        print('评论指数：' + str(s['score']) + '   内容：' + s['content'].replace("\n", ''), file=data)
                        k = k + 1
                        if k > 10:
                            break
                        time.sleep(random.uniform(1, 2))
                data.close()
                print('评论信息已经输出到\tD:\\'+str(self.urlMessage)+'.txt')
        print("-------------------------")

    def get_price(self, url):
        res = requests.get(url=url, headers=self.headers)
        res = res.content.decode().replace("jQuery697146(", "").replace(")", "")
        json_response = json.loads(res)
        print("商品价格：￥" + json_response['price']['op'])
        self.get_ious(self.urlIous)

    # def get_json(self, url):
    #     print(url)
    #     res = requests.get(url=url, headers=self.headers)
    #     print(res)
    #     json_response = res.json()
    #     print(json_response)
    #     commentCountStr = json_response["CommentsCount"][0]["CommentCountStr"]
    #     defaultGoodCountStr = json_response["CommentsCount"][0]["DefaultGoodCountStr"]
    #     goodRateShow = json_response["CommentsCount"][0]["GoodRateShow"]
    #     goodCountStr = json_response["CommentsCount"][0]["GoodCountStr"]
    #     generalCountStr = json_response["CommentsCount"][0]["GeneralCountStr"]
    #     poorCountStr = json_response["CommentsCount"][0]["PoorCountStr"]
    #
    #     print('评论总数量：' + commentCountStr)
    #     print("用户好评率：" + str(goodRateShow) + '%')
    #     print("默认好评数：" + defaultGoodCountStr)
    #     print("用户好评数：" + goodCountStr)
    #     print("用户中评数：" + generalCountStr)
    #     print("用户差评数：" + poorCountStr)

    # 解析函数
    def parse_html(self, html):
        # 正则表达式
        re_bds = '<div class="sku-name">(.*?)</div>'
        re_bdes = '<dl class="clearfix" style="margin:0">(.*?)</dl>'  # 详细信息查询
        re_bdes1 = '<ul class="parameter2 p-parameter-list">(.*?)</ul>'  # 基础信息查询
        re_merchants = 'clstag="shangpin|keycount|product|dianpuname1">(.*?)</a>'  # 商品名称
        re_merchant = '<div class="score-part">(.*?)</div>'  # 商家评分
        re_configuration = '<div id="choose-attrs" >(.*?)<div id="choose-results" class="li" style="display:none">'  # 选择参数
        pattern = re.compile(re_bds, re.S)
        patterns = re.compile(re_bdes, re.S)
        patterns1 = re.compile(re_bdes1, re.S)
        merchants = re.compile(re_merchants, re.S)
        merchant = re.compile(re_merchant, re.S)
        configuration = re.compile(re_configuration, re.S)
        r_list = pattern.findall(html)
        r_lists = patterns.findall(html)
        r_lists1 = patterns1.findall(html)
        r_merchants = merchants.findall(html)
        r_merchant = merchant.findall(html)
        r_configuration = configuration.findall(html)
        self.save_html(r_list, r_lists, r_lists1, r_merchants, r_merchant, r_configuration)

    def save_html(self, r_list, r_lists, r_list1, r_merchants, r_merchant, r_configuration):
        for r in r_list:
            s = r.strip().split('>')
            print("商品名称：" + s[len(s) - 1].strip())
        for r in r_configuration:
            s = r.strip().split('<div class="dt ">')
            k = 0
            for m in s:
                if k != 0:
                    color = m.split("</div>")
                    print(color[0].strip(), end='')
                    colors = m.split('<a href="#none" clstag="shangpin|keycount|product|yanse-')
                    i = 0
                    for c in colors:
                        if i != 0:
                            print('    ' + c.split('">')[0].strip(), end='')
                        i = i + 1
                    print()
                k = k + 1
        print("商品基础信息：")
        r_list1 = r_list1[0].split("</li>")
        for r in r_list1:
            s = r.strip().split("'>")
            if len(s) == 2:
                print('\t' + s[1].strip().replace("</li>", ''))
        if len(r_lists) != 0:
            print("商品详细信息：")
            for r in r_lists:
                s = r.strip().split("</dt><dd>")
                if len(s) != 2:
                    message = s[0].split("</dt>")
                    message2 = message[1].strip().split("p>")
                    print('\t' + message[0].strip().replace("<dt>", '') + ': ' + message2[1].strip().replace("</", ''))
                else:
                    print('\t' + s[0].strip().replace("<dt>", '') + ': ' + s[1].strip().replace("</dd>", ''))
        print("店铺信息：")
        for r in r_merchants:
            if len(r) > 0:
                print("\t店铺名称：" + r)
        for r in r_merchant:
            s = r.strip().split("\">")
            s1 = s[1].strip().split("</")
            s2 = s[3].strip().split("</")
            print('\t' + s1[0].strip() + '   ' + s2[0].strip())
        self.get_price(self.urlPrice)
        self.get_comments(self.urlComments)
        time.sleep(random.uniform(1, 2))

    # 主函数
    def run(self):
        self.get_html(self.url)


# 以脚本方式启动
if __name__ == '__main__':
    # 捕捉异常错误
    try:
        jing = JingDongPage()
        jing.run()
    except Exception as e:
        print("错误:", e)
